package com.zwb.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zwb.boot.common.R;
import com.zwb.boot.entity.AddressBook;
import com.zwb.boot.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author 头发又黑又长
 * @Date 2022/7/25 19:15
 * @email zwb15083976291@163.com
 */
@Api("地址铺管理")
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @ApiOperation("添加地址信息")
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook,
                          HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute("user");
        addressBook.setUserId(userId);
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, userId);
        // 如果不存在地址。就设置为默认地址
        long count = addressBookService.count(queryWrapper);
        if (count == 0) {
            addressBook.setIsDefault(1);
        }
        addressBookService.save(addressBook);
        return R.success("保存成功");
    }


    @ApiOperation("获取地址列表")
    @GetMapping("/list")
    public R<List<AddressBook>> list( HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq( AddressBook::getUserId, userId)
                .orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }

    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public R<String> setDefault(@RequestBody Map<String, Long> map) {
        Long id = map.get("id");
        if (ObjectUtils.isEmpty(id)) {
            return R.error("缺少关键参数");
        }
        addressBookService.setDefault(id);
        return R.success("修改成功");
    }

    @ApiOperation("获取默认地址")
    @GetMapping("/default")
    public R<Object> getDefault(HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getIsDefault, 1)
                .eq(AddressBook::getUserId, userId);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if (ObjectUtils.isEmpty(addressBook)) {
            return R.error("不存在默认地址");
        }
        return R.success(addressBook);
    }


    @ApiOperation("获取指定地址")
    @GetMapping("/{id}")
    public R<AddressBook> get(@PathVariable("id") Long id) {
        return R.success(addressBookService.getById(id));
    }

    @ApiOperation("修改地址信息")
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    @ApiOperation("删除地址")
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        addressBookService.removeBatchByIds(ids);
        return R.success("删除成功");
    }

}
